-- Model
function M(class)
	return require(config.script_dirs.model .. class);
end

-- Controller
function C(class)
	local c = config.script_dirs.controller .. class;
	if not os.exists(c .. '.lua') then 
		print("not exist " .. c)
		return 
	end;
	return require(c);
end

-- Vender
function T(class)
	return require(config.script_dirs.vendor .. class);
end

------------------------- router define----------------------------
-- entry
local entry = function(url, controller, action, req, rsp)
	template.template_root = config.script_dirs.view;

	req.controller	= controller;
	req.action	= action;

	-- check url
	local tb = C(controller);
	if not tb then 
		return rsp:error(404) 
	end;

	if not tb[action] then 
		return rsp:error(404) 
	end;

	-- call
	xpcall(function()
		tb[action](tb, url, controller, action, req, rsp);
	end, function(err)
		log.error(err);
		rsp:error(500);
	end);
end

-- HOME
router:get('^/$', function(req, rsp)
	log.error(url .. '...any33333222');
	entry('home', 'index', '', req, rsp);
end);


-- MVC
router:any('^/kk/bb/cc/dd/([A-Za-z][A-Za-z0-9_/]*)/([A-Za-z][A-Za-z0-9_]*)$', function(req, rsp, url, urlSuffix, urlTail)
	local suffixLen = string.len(urlSuffix .. urlTail) + 1;
	local controller = string.sub(url, 1, string.len(url) - suffixLen - 1);
	local action = string.gsub(controller, "/", "_")
	
	entry(url, controller, action, req, rsp);
end);

local multipath_entry = function(req, rsp, url, urlHead, urlSuffix, urlTail)
	local urlRrs = string.reverse(url);
	local posRrs = string.find(urlRrs, '/');
	local urlOfs = string.len(url) - posRrs;
	local controller = string.sub(url, 1, urlOfs);
	local action = string.sub(url, urlOfs + 2);
	entry(url, controller, action, req, rsp);
end

-- /home/aa/bb/cc/dd/ee/index
router:any('^/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)$', function(req, rsp, url, urlHead, urlSuffix, urlTail)	
	multipath_entry(req, rsp, url, urlHead, urlSuffix, urlTail);
end);
-- /home/aa/bb/cc/dd/index
router:any('^/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)$', function(req, rsp, url, urlHead, urlSuffix, urlTail)	
	multipath_entry(req, rsp, url, urlHead, urlSuffix, urlTail);
end);
-- /home/aa/bb/cc/index
router:any('^/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)$', function(req, rsp, url, urlHead, urlSuffix, urlTail)	
	multipath_entry(req, rsp, url, urlHead, urlSuffix, urlTail);
end);
-- /home/aa/bb/index
router:any('^/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)$', function(req, rsp, url, urlHead, urlSuffix, urlTail)	
	multipath_entry(req, rsp, url, urlHead, urlSuffix, urlTail);
end);


-- MVC
-- /home/aa/index
router:any('^/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)$', function(req, rsp, url, urlHead, urlSuffix)
	local controller = '/' .. urlHead .. '/' .. urlSuffix
	local action = string.sub(url, string.len(controller) + 2);
	entry(url, controller, action, req, rsp);
end);
-- MVC
-- /home/index
router:any('^/([A-Za-z][A-Za-z0-9_]*)/([A-Za-z][A-Za-z0-9_]*)$', function(req, rsp, url, controller, action)
	entry(url, controller, action, req, rsp);
end);


-- MVC（default ACTION）
-- /home
router:any('^/([A-Za-z][A-Za-z0-9_]*)$', function(req, rsp, url, controller)
	log.error(url .. 'any2');
	entry(url, controller, 'index', req, rsp);
end);

-- MVC（default ACTION, multi layer controller）
-- /home/
router:any('^/([A-Za-z][A-Za-z0-9_/]*)/$', function(req, rsp, url, controller)
	log.error(url .. 'any1');
	entry(url, controller, 'index', req, rsp);
end);
